import BirdScript from "./BirdScript";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BirdManger extends cc.Component {

	// 小鸟预设体
	@property(cc.Prefab)
	birdPre: cc.Prefab
	// 1s出现一只小鸟
	time: number = 1
	// 分数
	score: number = 0
	// 分数标签
	@property(cc.Label)
	scoreLabel: cc.Label
	@property(cc.Node)
	mainView: cc.Node

	// onLoad () {}

	start() {
		// 每隔一秒创建一只小鸟
		this.node.runAction(cc.repeatForever(cc.sequence(cc.delayTime(this.time), cc.callFunc(() => {
			// 创建小鸟
			let bird = cc.instantiate(this.birdPre)
			// 设置父物体
			bird.setParent(this.node)
			// 设置小鸟位置
			bird.y = this.node.y
			bird.x = Math.random() * 220 - 110
			// 飞
			bird.getComponent(BirdScript).fly()

			// 加分回调
			bird.getComponent(BirdScript).addScoreCallBack = () => {
				this.score += 100
				this.scoreLabel.string = this.score + ""
				// console.log("当前分数" + this.score)
			}

			// 死亡回调
			bird.getComponent(BirdScript).dieCallBack = () => {
				this.node.destroyAllChildren()
				this.node.stopAllActions()
				this.mainView.active = true
				console.log("die")
			}

		}))))
	}

	// 返回主页
	goMain() {
		cc.director.loadScene("start")
	}

	// update (dt) {}
}
